package api

import (
	"sieve_admin_server_userpass/pkg/json"
	"strconv"
)

type Member struct {
	Uid    int
	Name   string
	Email  string
	Dept   int
	Avatar string
}

func GetMembers() ([]*Member, error) {
	ret, err := reqApi("get_users", map[string]string{})
	if err != nil {
		return nil, err
	}
	list := []*Member{}
	if err = json.Unmarshal(ret, &list); err != nil {
		return nil, err
	}
	return list, nil
}

func GetAllMembers() ([]*Member, error) {
	ret, err := reqApi("get_all_users", map[string]string{})
	if err != nil {
		return nil, err
	}
	list := []*Member{}
	if err = json.Unmarshal(ret, &list); err != nil {
		return nil, err
	}
	return list, nil
}

type Dept struct {
	Did    int
	Name   string
	Leader int
}

func GetDepts() ([]*Dept, error) {
	ret, err := reqApi("get_depts", map[string]string{})
	if err != nil {
		return nil, err
	}
	list := []*Dept{}
	if err = json.Unmarshal(ret, &list); err != nil {
		return nil, err
	}
	return list, nil
}

func VerifySign(uid int, data, sign string) (bool, error) {
	ret, err := reqApi("verify_sign", map[string]string{
		"uid":  strconv.Itoa(uid),
		"data": data,
		"sign": sign,
	})
	if err != nil {
		return false, err
	}
	return string(ret) == "OK", nil
}

func GetUserPubKey(uid int) (string, error) {
	ret, err := reqApi("get_user_pubkey", map[string]string{
		"uid": strconv.Itoa(uid),
	})
	return string(ret), err
}

type DeptUser struct {
	Did    int
	Uid    int
	Name   string
	Leader int
	Title  string
}

func GetDeptUserList(did int) ([]*DeptUser, error) {
	ret, err := reqApi("get_dept_user_list", map[string]string{
		"did": strconv.Itoa(did),
	})
	if err != nil {
		return nil, err
	}
	list := []*DeptUser{}
	if err = json.Unmarshal(ret, &list); err != nil {
		return nil, err
	}
	return list, err
}
